﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace EDesk.kit
{
    class Wins
    {

        /// </summary>
        /// 根据标题和类名找句柄
        /// <summary>
        /// <param name="IpClassName">窗口类名 如果为"" 则只根据标题查找</param>
        /// <param name="IpClassName">窗口标题 如果为"" 则只根据类名查找</param>
        /// <returns>找不到则返回0</returns>
        public static int FindWindowHwnd(string IpClassName, string IpTitleName)
        {
            if (IpTitleName == "" && IpClassName != "")
            {
                return (int)FindWindow(IpClassName, null);
            }
            else if (IpClassName == "" && IpTitleName != "")
            {
                return (int)FindWindow(null, IpTitleName);
            }
            else if (IpClassName != "" && IpTitleName != "")
            {
                return (int)FindWindow(IpClassName, IpTitleName);
            }
            return 0;
        }

        /// <summary>
        /// 得到窗口上一级窗口的句柄
        /// </summary>
        /// <param name="ChildHwnd">子窗口句柄</param>
        /// <returns> 返回 窗口句柄 找不到返回 0</returns>
        public int GetChild_Host(int ChildHwnd)
        {
            return (int)GetParent((IntPtr)ChildHwnd);
        }

        /// <summary>
        /// 获得顶层窗口
        /// </summary>
        /// <returns>返回 窗口句柄</returns>
        public static int GetForeGroundWindow()
        {
            return (int)GetForegroundwindow();
        }

        /// <summary>
        /// 判断窗口是否存在
        /// </summary>
        /// <param name="Hwnd">窗口句柄</param>
        /// <returns>存在返回 true 不存在返回 false</returns>
        public static bool IsWindow(int Hwnd)
        {
            if (IsWindow((IntPtr)Hwnd))
            {
                return true;
            }
            return false;
        }

        /// </summary>
        /// 得到指定窗口类名
        /// <summary>
        /// <param name="hWnd">句柄</param>
        /// <returns>找不到返回""</returns>
        public static string GetClassName(int hWnd)
        {
            StringBuilder lpClassName = new StringBuilder(128);
            if (GetClassName((IntPtr)hWnd, lpClassName, lpClassName.Capacity) == 0)
            {
                return "";
            }
            return lpClassName.ToString();
        }

        /// </summary>
        /// 得到指定窗口标题
        /// <summary>
        /// <param name="hWnd">句柄</param>
        /// <returns>找不到返回""</returns>
        public static string GetTitleName(int hWnd)
        {
            StringBuilder title = new StringBuilder(256);
            if (GetWindowText((IntPtr)hWnd, title, title.Capacity) == 0)
            {
                return "";
            }
            return title.ToString();
        }

        /// <summary>
        /// 显示或隐藏窗体
        /// </summary>
        /// <param name="Hwnd"></param>
        /// <param name="display"></param>
        /// <returns></returns>
        public static int DisplayForm(int Hwnd, bool display)
        {
            return (int)ShowWindow((IntPtr)Hwnd, display ? 5 : 0);
        }

        /// <summary>
        /// 设置窗口标题
        /// </summary>
        /// <param name="Hwnd">窗口句柄</param>
        /// <param name="newtext">新标题</param>
        public static void SetWindowText(int Hwnd, string newtext)
        {
            SetWindowText((IntPtr)Hwnd, newtext);
        }




        /****************************************************************************************************/




        // 根据标题和类名找句柄
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);

        //获得父窗口
        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        private static extern IntPtr GetParent(IntPtr hWnd);

        //获得顶层窗口
        [DllImport("user32", EntryPoint = "GetForegroundWindow")]
        private static extern IntPtr GetForegroundwindow();

        //判断窗口是否存在
        [DllImport("user32", EntryPoint = "IsWindow")]
        private static extern bool IsWindow(IntPtr hWnd);

        // 得到指定窗口类名
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        // 返回窗口标题
        [DllImport("user32", SetLastError = true)]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        //修改指定窗口标题
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        private static extern int SetWindowText(IntPtr hWnd, string text);

        //显示或隐藏窗体
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        //系统参数
        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(int uAction, int uParam, ref Rectangle lpvParam, int fuWinIni);

    }
}
